package edu.unc.ils.pos;

import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections15.Bag;
import org.apache.commons.collections15.bag.HashBag;

import com.aliasi.hmm.HiddenMarkovModel;
import com.aliasi.hmm.HmmDecoder;
import com.aliasi.tokenizer.RegExTokenizerFactory;
import com.aliasi.tokenizer.Tokenizer;
import com.aliasi.util.Streams;

public class POSFilter {

	List<String> allowedPOS = new ArrayList<String>();
	HmmDecoder decoder;
	
	public void setAllowedPOS(List<String> allowedPOS)
	{
		this.allowedPOS = allowedPOS;
	}
	
	public POSFilter() throws Exception 
	{
		String model = "/Users/cwillis/dev/hive/hive-data/lingpipe/postagger/models/pos-en-general-brown.HiddenMarkovModel";
		FileInputStream fis = new FileInputStream(model);
		ObjectInputStream ois = new ObjectInputStream(fis);
		HiddenMarkovModel hmm = (HiddenMarkovModel) ois.readObject();
		Streams.closeInputStream(ois);
		decoder = new HmmDecoder(hmm);
	}
	
    public POSFilter(HmmDecoder decoder) throws Exception 
    {
        this.decoder = decoder;
    }

	public String applyFilter(String text) throws Exception 
	{

		
		Bag<String> terms = new HashBag<String>();
		String[] tokens = getTokens(text);
		String[] tags = decoder.firstBest(tokens);
		StringBuffer filteredText = new StringBuffer();
		for (int i=0; i<tokens.length; i++)
		{
			String term = tokens[i];
			String tag = tags[i];
			if (allowedPOS.size() == 0 || allowedPOS.contains(tag))
			{
				if (i > 0)
					filteredText.append(' ');
				
				filteredText.append(term);
			}
		}
		
		return filteredText.toString();
	}
	
	public String[] getTokens(String text) {
		char[] cs = text.toCharArray();
		Tokenizer tokenizer = new RegExTokenizerFactory("(-|'|\\d|\\p{L})+|\\S").tokenizer(cs, 0, cs.length);
		
		return tokenizer.tokenize();
	}
	
}
